#include <stdio.h>
#include "DS1302.h"

#define ds1302_delay()  delay_us(50) // 延时函数，单位us

void sclk_high(void)
{
    GPIO_WritePin(SCL_PORT, SCL_PIN, GPIO_PIN_SET);
    ds1302_delay();
}
void sclk_low(void)
{
    GPIO_WritePin(SCL_PORT, SCL_PIN, GPIO_PIN_RESET);
    ds1302_delay();
}
void io_high(void)
{
    GPIO_WritePin(IO_PORT, IO_PIN, GPIO_PIN_SET);
    ds1302_delay();
}
void io_low(void)
{
    GPIO_WritePin(IO_PORT, IO_PIN, GPIO_PIN_RESET);
    ds1302_delay();
}
int io_read(void)
{
    ds1302_delay();
    return GPIO_ReadPin(IO_PORT, IO_PIN);
}
void io_in(void)
{
    IO_IN();
}
void io_out(void)
{
    IO_OUT();
}
void ce_high(void)
{
    GPIO_WritePin(CE_PORT, CE_PIN, GPIO_PIN_SET);
    ds1302_delay();
}
void ce_low(void)
{
    GPIO_WritePin(CE_PORT, CE_PIN, GPIO_PIN_RESET);
    ds1302_delay();
}

void ds1302_delay_us(uint32_t us)
{
    delay_us(us);
}

int main(void)
{
    struct tm test_time = {
        .tm_sec   = 55,
        .tm_min   = 59,  
        .tm_hour  = 23,  
        .tm_mday  = 31,  
        .tm_mon   = 12 - 1,  
        .tm_wday  = 0,  
        .tm_year  = 2099 - 1900 
    };
    ds1302_gpio_ops_t ops = {
        .sclk_high = sclk_high,
        .sclk_low = sclk_low,
        .io_high = io_high,
        .io_low = io_low,
        .io_read = io_read,
        .io_in = io_in,
        .io_out = io_out,
        .ce_high = ce_high,
        .ce_low = ce_low,
        .delay_us = ds1302_delay_us
    };
    ds1302_set_gpio_ops(&ops);
    ds1302_set_time(&test_time);
    ds1302_get_time(&test_time);
    printf("Current time: %04d-%02d-%02d %02d:%02d:%02d\r\n", 
           test_time.tm_year + 1900, test_time.tm_mon + 1, test_time.tm_mday, 
           test_time.tm_hour, test_time.tm_min, test_time.tm_sec);
    // ds1302_init();
    return 0;
}